package com.yss.reportworld.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.core.exception.base.BaseException;
import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.reportworld.domain.Table;
import com.yss.reportworld.domain.TpGlExhibDatadict;
import com.yss.reportworld.service.*;
import com.yss.reportworld.util.TableInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.reportworld.domain.TableDetail;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * 表明细信息Controller
 *
 * @author yss
 * @date 2023-03-20
 */
@RestController
@RequestMapping("/detail")
public class TableDetailController extends BaseController {
    @Autowired
    private ITableDetailService tableDetailService;
    @Autowired
    private ITableService tableService;
    @Autowired
    private IModularService modularService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IReportService reportService;
    @Autowired
    private ITpGlExhibDatadictService tpGlExhibDatadictService;
    @Autowired
    private ISqlScriptService sqlScriptService;

    /**
     * 查询表明细信息列表
     */
    @RequiresPermissions("reportworld:detail:list")
    @GetMapping("/list")
    public TableDataInfo list(TableDetail tableDetail) {
        startPage();
        List<TableDetail> list = tableDetailService.selectTableDetailList(tableDetail, true);
        return getDataTable(list);
    }


    /**
     * 初始化 加载
     */
    @RequiresPermissions("reportworld:detail:list")
    @Log(title = "表明细信息", businessType = BusinessType.OTHER)
    @PostMapping("/init")
    public AjaxResult init(@RequestBody TableDetail tableDetail) {
        try {
            TableInfoUtil.handleTableVersion(tableDetail.getHisVersion()); // 替换表名
            AjaxResult ajax = AjaxResult.success();
            //加载所属模块，客户，报表
            ajax.put("modularTree", modularService.selectModularTreeList());
            ajax.put("customerTree", customerService.selectCustomerTreeList());
            ajax.put("reportTree", reportService.selectReportTreeList());
            return ajax;
        } catch (Exception ex) {
            throw new BusinessException("初始化加载数据出错，请稍后重试！" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    /**
     * 初始化 加载
     */
    @RequiresPermissions("reportworld:detail:list")
    @Log(title = "表明细信息", businessType = BusinessType.OTHER)
    @PostMapping("/initDeatil")
    public AjaxResult initDeatil(@RequestBody TableDetail tableDetail) {
        try {
            AjaxResult ajax = AjaxResult.success();
            TpGlExhibDatadict tpGlExhibDatadict = new TpGlExhibDatadict();
            tpGlExhibDatadict.setHisVersion(tableDetail.getHisVersion());
            ajax.put("tpGlExhibDatadicts", tpGlExhibDatadictService.queryTpGlExhibDatadictGroupByClassId(tpGlExhibDatadict));
            ajax.put("hisVersions", sqlScriptService.getReductionVersion(false));
            TableInfoUtil.handleTableVersion(tableDetail.getHisVersion()); // 替换表名
            //加载所属模块，客户，报表
            ajax.put("modularTree", modularService.selectModularTreeList());
            ajax.put("customerTree", customerService.selectCustomerTreeList());
            ajax.put("reportTree", reportService.selectReportTreeList());
            return ajax;
        } catch (Exception ex) {
            throw new BusinessException("初始化加载数据出错，请稍后重试！" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    /**
     * 获取所有表字段信息
     *
     * @return
     */
    @RequiresPermissions("reportworld:detail:query")
    @Log(title = "表明细信息", businessType = BusinessType.OTHER)
    @PostMapping("/getInfo")
    public AjaxResult getInfo(@RequestBody TableDetail tableDetail) {
        AjaxResult ajax = AjaxResult.success();
        // 加载表字段信息
        ajax.put("tableColumns", tableDetailService.selectTableDetailList(tableDetail, false));
        TpGlExhibDatadict tpGlExhibDatadict = new TpGlExhibDatadict();
        tpGlExhibDatadict.setHisVersion(tableDetail.getHisVersion());
        ajax.put("tpGlExhibDatadicts", tpGlExhibDatadictService.queryTpGlExhibDatadictGroupByClassId(tpGlExhibDatadict));
        return ajax;
    }

    /**
     * 获取表信息
     *
     * @return
     */
    @PostMapping("/getInfoDetail")
    public AjaxResult getInfoDetail(@RequestBody TableDetail tableDetail) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isEmpty(tableDetail.getTableId())) { //添加的时候先加载表，选择后加载字段信息
            Table table = new Table();
            table.setHisVersion(tableDetail.getHisVersion());
            ajax.put("tables", tableService.selectTableList(table, false));
        } else {
            // 加载表字段信息 选择来源字段使用
            ajax.put("tableColumns", tableDetailService.selectTableDetailList(tableDetail, false));
        }
        return ajax;
    }


    /**
     * 导出表明细信息列表
     */
    @RequiresPermissions("reportworld:detail:export")
    @Log(title = "表明细信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TableDetail tableDetail) {
        List<TableDetail> list = tableDetailService.selectTableDetailList(tableDetail, true);
        EasyExeclUtil.export(response, list, TableDetail.class);
    }

    /**
     * 获取表明细信息详细信息
     */
    @RequiresPermissions("reportworld:detail:query")
    @PostMapping(value = "/getTableDetailInfo")
    public AjaxResult getTableDetailInfo(@RequestBody TableDetail tableDetail) {
        return success(tableDetailService.selectTableDetailById(tableDetail));
    }

    /**
     * 新增表明细信息
     */
    @RequiresPermissions("reportworld:detail:add")
    @Log(title = "表明细信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated TableDetail tableDetail) {
        tableDetailService.addTableDetail(tableDetail);
        return success();
    }

    /**
     * 修改表明细信息
     */
    @RequiresPermissions("reportworld:detail:edit")
    @Log(title = "表明细信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated TableDetail tableDetail) {
        tableDetailService.editTableDetail(tableDetail);
        return success();
    }

    /**
     * 删除表明细信息
     */
    @RequiresPermissions("reportworld:detail:remove")
    @Log(title = "表明细信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody TableDetail tableDetail) {
        return toAjax(tableDetailService.delReportTableDetail(tableDetail));
    }

    /**
     * 保存表顺序信息
     */
    @RequiresPermissions("reportworld:detail:orderNum")
    @Log(title = "表明细信息", businessType = BusinessType.UPDATE)
    @PostMapping("/handleUpdateOrderNum")
    public AjaxResult handleUpdateOrderNum(@RequestBody String josn) {
        List<TableDetail> list = JSONArray.parseArray(josn, TableDetail.class);
        if (StringUtils.isNotEmpty(list)) {
            tableDetailService.handleUpdateOrderNum(list);
        }
        return success();
    }


    /**
     * 批量修改字段表信息
     */
    @RequiresPermissions("reportworld:detail:batchEdit")
    @Log(title = "表信息", businessType = BusinessType.UPDATE)
    @PostMapping("/handleUpdateBatch")
    public AjaxResult handleUpdateBatch(@RequestBody TableDetail tableDetail) {
        AjaxResult ajax = AjaxResult.success();
        tableDetailService.batchUpdateTableInfoDetail(tableDetail);
        return ajax;
    }

}
